﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OmronEdit
{
    public partial class OmronEdit : TextBox
    {
        private bool _nonDigitEntered;
        //private int minErr, memAddr;

        public OmronEdit()
        {
            InitializeComponent();
        }

        public int MinError { get; set; }
        public int MaxError { get; set; }

        public float Step { get; set; }


        private void OmronEditKeyPress(object sender, KeyPressEventArgs e)
        {
            /*
            if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '-' && e.KeyChar!='.' )
                e.Handled = true;
            
            if (SelectionStart != 0 && e.KeyChar == '-')
                e.Handled = true;
           s
            */
            if (e.KeyChar == ',')
            {
                e.Handled = true;
                if (Text.IndexOf('.') < 0 && this.SelectionStart!=0)
                {
                    Text += ".";
                    SelectionStart = Text.Length;
                }

            }
            if ((Text.IndexOf('.') >= 0 || this.SelectionStart == 0) && e.KeyChar == '.' )
                e.Handled = true;
            if (this.SelectionStart != 0 && e.KeyChar == '-')
                e.Handled = true;
            if (_nonDigitEntered)
                e.Handled = true;

        }

        private void OmronEditKeyDown(object sender, KeyEventArgs e)
        {
            _nonDigitEntered=false;
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    if (e.KeyCode != Keys.Back && e.KeyCode!=Keys.OemPeriod 
                        && e.KeyCode!=Keys.Oemcomma && e.KeyCode!=Keys.OemMinus
                        && e.KeyCode!=Keys.Decimal && e.KeyCode!=Keys.Subtract)
                        _nonDigitEntered = true;
                    
                }
            }





        }

    }
}
